﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using SolutionExtender.Interfaces;
using SolutionExtender.Plugins;

namespace SolutionExtender.Controls
{
    public partial class FeatureSelectionControl : UserControl
    {
        #region Variables

        /// <summary>
        /// IOrganization service
        /// </summary>
        private readonly IOrganizationService _service;

        /// <summary>
        /// Plugins manager
        /// </summary>
        private readonly PluginManager _pManager;

        #endregion Variables

        #region Constructor

        /// <summary>
        /// Initializes a new instance of class FeatureSelectionControl
        /// </summary>
        /// <param name="service">IOrganization service</param>
        public FeatureSelectionControl(IOrganizationService service, PluginManager pManager)
        {
            InitializeComponent();

            _service = service;
            _pManager = pManager;

            pnlControls.HorizontalScroll.Visible = false;
            pnlControls.HorizontalScroll.Enabled = false;
        }

        #endregion Constructor

        #region Methods

        private void FeatureSelectionControlLoad(object sender, System.EventArgs e)
        {
            int x = 9;
            int y = 9;

            foreach(var plugin in _pManager.Plugins.OrderBy(p=>p.Sequence))
            {
                var connector = (IConnector)PluginManager.CreateInstance(plugin.AssemblyPath, plugin.FullName, new object[] { _service });

                var cBox = new CheckBox
                               {
                                   Text = connector.ObjectName,
                                   Tag = connector,
                                   Height = 24,
                                   Width = pnlControls.Width - 10,
                                   Location = new Point(x, y),
                                   Font = new Font("Segoe UI", 11F, FontStyle.Regular, GraphicsUnit.Point, 0)
                               };

                var label = new System.Windows.Forms.Label()
                                {
                                    Text = connector.Description,
                                    Location = new Point(x, y + 24),
                                    Height = 20,
                                    Width = pnlControls.Width - 10,
                                    Font = new Font("Segoe UI", 8.5F, FontStyle.Regular, GraphicsUnit.Point, 0)
                                };

                pnlControls.Controls.Add(cBox);
                pnlControls.Controls.Add(label);

                y += 43;
            }
        }

        /// <summary>
        /// Gets list of controls to process
        /// </summary>
        /// <returns>List of controls</returns>
        public List<IExportControl> GetControlsToLoad()
        {
            var controls = new List<IExportControl>();

            foreach (var ctrl in pnlControls.Controls)
            {
                if (ctrl is CheckBox)
                {
                    var cBox = (CheckBox) ctrl;

                    if (cBox.Checked)
                    {
                        controls.Add(((IConnector) cBox.Tag).GetExportControl(_service));
                    }
                }
            }

            if (controls.Count == 0)
            {
                pnlError.Visible = true;
                return null;
            }

            return controls;
        }

        public void TickCheckbox(object sender, System.EventArgs e)
        {
            pnlError.Visible = false;
        }

        #endregion Methods
    }
}
